package com.longjiyi.test;

import java.security.Key;
import java.util.*;

public class 字母异位词分组 {

    public static void main(String[] args) {
        String[] strings = new String[2];
        strings[0] = "tan";
        strings[1] = "nat";

        System.out.println(groupAnagrams(strings));
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> arrayList = new ArrayList<>();
        HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String value = String.valueOf(chars);
            if (!hashMap.containsKey(value)) {
                hashMap.put(value, new ArrayList<>());
            }
            hashMap.get(value).add(str);
        }
        for (Map.Entry<String, ArrayList<String>> entry : hashMap.entrySet()) {
            arrayList.add(entry.getValue());
        }
        return arrayList;
    }
}



